<template>
  <div class="main-layout">
    <!-- 顶部导航栏 -->
    <header class="header">
      <div class="header-left">
        <el-button @click="toggleSidebar" plain text class="toggle-btn">
          <el-icon><menu /></el-icon>
        </el-button>
        <h1 class="logo">学生管理系统</h1>
      </div>
      <div class="header-right">
        <el-dropdown>
          <span class="user-info">
            <el-avatar>用户</el-avatar>
            <span>{{ username }}</span>
          </span>
          <template #dropdown>
            <el-dropdown-menu>
              <el-dropdown-item>个人信息</el-dropdown-item>
              <el-dropdown-item>设置</el-dropdown-item>
              <el-dropdown-item divided @click="handleLogout">退出登录</el-dropdown-item>
            </el-dropdown-menu>
          </template>
        </el-dropdown>
      </div>
    </header>
    <!-- 主体内容 -->
    <div class="main-content">
      <!-- 侧边栏 -->
      <aside :class="['sidebar', { 'collapsed': isSidebarCollapsed }]">
        <el-menu
          :default-active="activeMenu"
          class="el-menu-vertical-demo"
          @open="handleOpen"
          @close="handleClose"
          router
        >
          <el-menu-item index="">
            <el-icon><home /></el-icon>
            <span>首页</span>
          </el-menu-item>
          <el-menu-item index="students">
            <el-icon><user /></el-icon>
            <span>学生管理</span>
          </el-menu-item>
          <el-menu-item index="classes">
            <el-icon><school /></el-icon>
            <span>班级管理</span>
          </el-menu-item>
          <el-menu-item index="courses">
            <el-icon><document /></el-icon>
            <span>课程管理</span>
          </el-menu-item>
        </el-menu>
      </aside>
      <!-- 内容区域 -->
      <main class="content">
        <router-view />
      </main>
    </div>
  </div>
</template>

<script setup>
import { ref, computed, onMounted } from 'vue'
import { useRouter } from 'vue-router'
import * as Icons from '@element-plus/icons-vue'
const { Menu, Home, User, School, Document } = Icons

const router = useRouter()
const isSidebarCollapsed = ref(false)
const username = ref('管理员')

// 获取当前活动的菜单
const activeMenu = computed(() => {
  const path = router.currentRoute.value.path
  if (path === '/') return ''
  return path.replace('/', '')
})

// 切换侧边栏
const toggleSidebar = () => {
  isSidebarCollapsed.value = !isSidebarCollapsed.value
}

// 退出登录
const handleLogout = () => {
  localStorage.removeItem('token')
  localStorage.removeItem('userInfo')
  router.push('/login')
}

// 菜单展开收起处理
const handleOpen = (key, keyPath) => {
  console.log(key, keyPath)
}

const handleClose = (key, keyPath) => {
  console.log(key, keyPath)
}

// 页面加载时获取用户信息
onMounted(() => {
  const userInfo = localStorage.getItem('userInfo')
  if (userInfo) {
    try {
      const parsedInfo = JSON.parse(userInfo)
      username.value = parsedInfo.username || '管理员'
    } catch (e) {
      console.error('解析用户信息失败:', e)
    }
  }
})
</script>

<style scoped>
.main-layout {
  height: 100vh;
  display: flex;
  flex-direction: column;
  background-color: #f0f2f5;
}

.header {
  height: 60px;
  background-color: #fff;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.1);
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0 20px;
  z-index: 100;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 16px;
}

.toggle-btn {
  width: 40px;
  height: 40px;
}

.logo {
  font-size: 20px;
  font-weight: bold;
  color: #1890ff;
}

.header-right {
  display: flex;
  align-items: center;
}

.user-info {
  display: flex;
  align-items: center;
  gap: 8px;
  cursor: pointer;
}

.main-content {
  flex: 1;
  display: flex;
  overflow: hidden;
}

.sidebar {
  width: 200px;
  background-color: #fff;
  box-shadow: 2px 0 8px rgba(0, 0, 0, 0.1);
  transition: width 0.3s ease;
  overflow: hidden;
}

.sidebar.collapsed {
  width: 64px;
}

.sidebar .el-menu {
  height: 100%;
  border-right: none;
}

.sidebar.collapsed .el-menu-item__content {
  display: none;
}

.content {
  flex: 1;
  padding: 20px;
  overflow-y: auto;
}
</style>